﻿using System;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.DataTier;

public partial class SysDictionary_Frame : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            BuildTree();
        }        
    }

    private void BuildTree()
    {
        DictionaryTree.Nodes.Clear();
        TreeNode chnd = new TreeNode();
        chnd.Text = "<a class=TreeNode ID=dSiteMap_  href=\"#\"  onclick=\"showTreeBranch('iFarmeAdmin_SiteMap.aspx','dSiteMap_');\"> Карта сайта </a>";        
        DictionaryTree.Nodes.Add(chnd);

        chnd = new TreeNode();
        chnd.Text = "<a class=TreeNode ID=dMap_  href=\"#\"  onclick=\"showTreeBranch('iFrameAdmin_TreeDictionaryMap.aspx','dMap_ ');\"> Карта справочников </a>";
        DictionaryTree.Nodes.Add(chnd);
        foreach (TreeNode n in GetTree())
        {
            DictionaryTree.Nodes.Add(n);
        }

    }

    /// <summary>
    /// Вернуть дерево категорий справочников
    /// </summary>
    /// <returns></returns>
    public TreeNode[] GetTree()
    {
        TreeNode[] arr = new TreeNode[2];
        arr[0] = new TreeNode();
        arr[0].Text = "<a class=TreeNode> Системные </a>";
        arr[1] = new TreeNode();
        arr[1].Text = "<a class=TreeNode> Настраиваемые </a>";
        SetNodesUrl(arr[0], GlobalConstants.TreeDictionaryFolderType_System, "sys");
        SetNodesUrl(arr[1], GlobalConstants.CurrentAgencyId, "user");
        return arr;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="nd"></param>
    /// <param name="FolderTypeID">Тип справочника (провайдера / пользовательский) </param>
    private void SetNodesUrl(TreeNode nd, string FolderTypeID, string pref)
    {

        wsDictionary ws = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary];
        TreeDictionaryTypeList lst = ws.GetTreeDictionaryTypeList();        

        foreach (TreeDictionaryTypeList_Item   i in lst)
        {

            if (pref == "user" && i.IsSystem == 1) continue;
            if (pref != "user" && i.IsSystem != 1) continue;
 
            TreeNode Child = new TreeNode();

            string NodeId = pref + i.ID.Replace("-", string.Empty);

            switch (i.ID)
            {
                case "991F97B3-6314-413D-9FD1-24157CF0479D": // Список адресных программ
                    Child.Text = "<a class=TreeNode ID=" + NodeId + " href=\"#\" onclick=\"showTreeBranch('iFarmeDictionary_AdressPrograms.aspx?FolderID=" + FolderTypeID + "','" + NodeId + "');\">  " + i.Value + " </a>";
                    break;

                default:
                    Child.Text = "<a class=TreeNode  ID=" + NodeId + " href=\"#\"  onclick=\"showTreeBranch('iFarmeDictionary_TreeDictonary.aspx?TreeDictionaryID=" + i.ID + "&FolderID=" + FolderTypeID + "','" + NodeId + "');\">  " + i.Value + " </a>";
                    break;
            }
            nd.ChildNodes.Add(Child);
        }

    }

    


}
